package com.mxx.pmxt.job;

import com.mxx.pmxt.dao.GoodsModelMapper;
import com.mxx.pmxt.dao.PUsersModelMapper;
import com.mxx.pmxt.dao.UserGoodsModelMapper;
import com.mxx.pmxt.model.GoodsModel;
import com.mxx.pmxt.model.PUsersModel;
import com.mxx.pmxt.model.UserGoodsModel;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by 马潇霄 on 2017/4/26.
 */
public class OrderTimer
{
    @Resource
    GoodsModelMapper goodsModelMapper;

    @Resource
    UserGoodsModelMapper userGoodsModelMapper;

    @Resource
    PUsersModelMapper pUsersModelMapper;

    private Logger log = Logger.getLogger(OrderTimer.class);

    /*public void test()
    {
        System.out.print("测试");
    }*/

    public void checkpmend()
    {
        List<GoodsModel> goodsModels = goodsModelMapper.selectAllAfterCheck();
        Date date = new Date();
        for (GoodsModel goodsModel:goodsModels)
        {
            Date endtime = goodsModel.getGendtime();
            if (endtime.before(date)){
                //如果当前时间超过了拍卖结束时间
                goodsModel.setGstate(2);
                //将此商品的状态定为2：拍卖结束
                BigDecimal maxprice = userGoodsModelMapper.selectMaxPriceByGid(goodsModel.getGid());
                //根据gid查出此商品此时最大价格
                UserGoodsModel userGoodsModel = userGoodsModelMapper.selectMaxPrice(maxprice,goodsModel.getGid()).get(0);
                //根据最大价格查出此商品最大价格的出价人
                userGoodsModel.setPisget(1);
                //把此人定为此商品的买家
                userGoodsModelMapper.updateByPrimaryKeySelective(userGoodsModel);
                //更新关系表状态
                String getUser = userGoodsModel.getPuser();
                goodsModel.setGbuyer(getUser);
                //将此商品的购买人设为此人
                goodsModelMapper.updateByPrimaryKeySelective(goodsModel);
                //更新商品表

                int size =  userGoodsModelMapper.countByGid(goodsModel.getGid());//几个人拍
                if (size!=1){
                    PUsersModel owner = pUsersModelMapper.getUserByName(goodsModel.getGowner()).get(0);//卖家
                    BigDecimal ownerAcc = owner.getUaccount();
                    ownerAcc = ownerAcc.add(maxprice);
                    owner.setUaccount(ownerAcc);
                    pUsersModelMapper.updateByPrimaryKeySelective(owner);//卖家收费
                }

                List<String> notGetUsers= userGoodsModelMapper.selectUsersExMaxMin(goodsModel.getGbuyer(),goodsModel.getGid(),goodsModel.getGowner());
                //查出所有没拍到商品的用户列表（除了发布人）
                for (String notGetUser: notGetUsers) {

                    PUsersModel notGet = pUsersModelMapper.getUserByName(notGetUser).get(0);//根据用户名查出用户
                    BigDecimal notAccount = notGet.getUaccount();
                    notAccount=notAccount.add(userGoodsModelMapper.selectMaxPriceByGidUser(goodsModel.getGid(),notGetUser));//这个用户最大出价
                    notGet.setUaccount(notAccount);
                    pUsersModelMapper.updateByPrimaryKeySelective(notGet);//退回押金
                }


//                List<UserGoodsModel> disuserGoodsModels = userGoodsModelMapper.selectByGidState(goodsModel.getGid(),0);//未拍到此件商品的人
//                for (UserGoodsModel usergoods:disuserGoodsModels) {
//                    PUsersModel pUsersModel = pUsersModelMapper.getUserByName(usergoods.getPuser()).get(0);
//                    pUsersModel.setUaccount(usergoods.);
//                }
                log.info("============================商品"+goodsModel.getGname()+"拍卖结束============================================");
            }
        }

    }
}
